﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace HappyPlus.CommonUtils
{
    /// <summary>
    /// 类型相关的通用API
    /// </summary>
    public class TypeUtils
    {
        /// <summary>
        /// 检查基础类型，并返回BCL基础类型的字符串
        /// </summary>
        /// <remarks>
        /// 暂时主要用于检查Excel中配置的基础数据类型
        /// 补充数组类型
        /// </remarks>
        public static string CheckBaseType(string typeString)
        {
            if(string.IsNullOrEmpty(typeString))
            {
                return string.Empty;
            }

            var str = typeString.Trim().ToLower();
            if (str == "short" || str == "int16")
            {
                return "short";
            }
            else if (str == "int" || str == "int32")
            {
                return "int";
            }
            else if (str == "long" || str == "int64")
            {
                return "long";
            }
            else if (str == "float")
            {
                return "float";
            }
            else if (str == "double")
            {
                return "double";
            }
            else if (str == "ushort" || str == "uint16")
            {
                return "ushort";
            }
            else if (str == "uint" || str == "uint32")
            {
                return "uint";
            }
            else if (str == "ulong" || str == "uint64")
            {
                return "ulong";
            }
            else if (str == "string" || str == "str")
            {
                return "string";
            }
            else if (str == "boolean" || str == "bool")
            {
                return "bool";
            }
            else if (str == "char")
            {
                return "char";
            }
            else if (str == "decimal")
            {
                return "decimal";
            }
            else if (str == "short[]" || str == "int16[]")
            {
                return "List<short>";
            }
            else if (str == "int[]" || str == "int32[]")
            {
                return "List<int>";
            }
            else if (str == "long[]" || str == "int64[]")
            {
                return "List<long>";
            }
            else if (str == "float[]")
            {
                return "List<float>";
            }
            else if (str == "double[]")
            {
                return "List<double>";
            }
            else if (str == "ushort[]" || str == "uint16[]")
            {
                return "List<ushort>";
            }
            else if (str == "uint[]" || str == "uint32[]")
            {
                return "List<uint>";
            }
            else if (str == "ulong[]" || str == "uint64[]")
            {
                return "List<ulong>";
            }
            else if (str == "string[]" || str == "str[]")
            {
                return "List<string>";
            }
            else if (str == "char[]")
            {
                return "List<char>";
            }
            else if (str == "decimal[]")
            {
                return "List<decimal>";
            }

            return typeString;
        }

        /// <summary>
        /// 根据基础类型的字符串返回其实际类型
        /// </summary>
        public static Type GetBaseType(string typeStr)
        {
            if(string.IsNullOrEmpty(typeStr))
            {
                return null;
            }

            if (typeStr == "short")
            {
                return typeof(short);
            }
            else if (typeStr == "int")
            {
                return typeof(int);
            }
            else if (typeStr == "long")
            {
                return typeof(long);
            }
            else if (typeStr == "float")
            {
                return typeof(float);
            }
            else if (typeStr == "double")
            {
                return typeof(double);
            }
            else if (typeStr == "ushort")
            {
                return typeof(ushort);
            }
            else if (typeStr == "uint")
            {
                return typeof(uint);
            }
            else if (typeStr == "ulong")
            {
                return typeof(ulong);
            }
            else if (typeStr == "string")
            {
                return typeof(string);
            }
            else if (typeStr == "bool")
            {
                return typeof(bool);
            }
            else if (typeStr == "char")
            {
                return typeof(char);
            }
            else if (typeStr == "decimal")
            {
                return typeof(decimal);
            }
            else if (typeStr == "List<short>")
            {
                return typeof(List<short>);
            }
            else if (typeStr == "List<int>")
            {
                return typeof(List<int>);
            }
            else if (typeStr == "List<long>")
            {
                return typeof(List<long>);
            }
            else if (typeStr == "List<float>")
            {
                return typeof(List<float>);
            }
            else if (typeStr == "List<double>")
            {
                return typeof(List<double>);
            }
            else if (typeStr == "List<ushort>")
            {
                return typeof(List<ushort>);
            }
            else if (typeStr == "List<uint>")
            {
                return typeof(List<uint>);
            }
            else if (typeStr == "List<ulong>")
            {
                return typeof(List<ulong>);
            }
            else if (typeStr == "List<string>")
            {
                return typeof(List<string>);
            }
            //else if (typeStr == "bool")
            //{
            //    return typeof(bool);
            //}
            else if (typeStr == "List<char>")
            {
                return typeof(List<char>);
            }
            else if (typeStr == "List<decimal>")
            {
                return typeof(List<decimal>);
            }

            return null;
        }

        /// <summary>
        /// 查找所有assembly获取指定类型
        /// </summary>
        public static Type GetType(string className)
        {
            Type result = null;
            var assemblies = AppDomain.CurrentDomain.GetAssemblies();
            foreach (var assembly in assemblies)
            {
                Type t = assembly.GetType(className);                
                if(t != null)
                {
                    result = t;
                }
            }

            return result;
        }
    }
}